package com.simple.mrchip.app;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.simple.mrchip.core.HttpAccess;

public class AppActivity extends AppCompatActivity
    implements AttendFragment.OnFragmentInteractionListener,
        HistoryFragment.OnFragmentInteractionListener,
        ProfileFragment.OnFragmentInteractionListener{

    private Fragment attendFragment;
    private Fragment historyFragment;
    private Fragment profileFragment;
    private FragmentManager mFragmentManager;
    private FragmentTransaction mFragmentTransaction;

    private HttpAccess mHttpAccess;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    if (attendFragment == null) {
                        attendFragment = new AttendFragment();
                    }
                    mFragmentTransaction = mFragmentManager.beginTransaction();
                    mFragmentTransaction.replace(R.id.fragment_container, attendFragment);
                    mFragmentTransaction.commit();
                    return true;
                case R.id.navigation_dashboard:
                    if (historyFragment == null) {
                        historyFragment = new HistoryFragment();
                    }
                    mFragmentTransaction = mFragmentManager.beginTransaction();
                    mFragmentTransaction.replace(R.id.fragment_container, historyFragment);
                    mFragmentTransaction.commit();
                    return true;
                case R.id.navigation_notifications:
                    if (profileFragment == null) {
                        profileFragment = new ProfileFragment();
                    }
                    mFragmentTransaction = mFragmentManager.beginTransaction();
                    mFragmentTransaction.replace(R.id.fragment_container, profileFragment);
                    mFragmentTransaction.commit();
                    return true;
                default:
                    break;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app);
        mFragmentManager = getSupportFragmentManager();
        if (attendFragment == null) {
            attendFragment = new AttendFragment();
        }
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.replace(R.id.fragment_container, attendFragment);
        mFragmentTransaction.commit();
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        mHttpAccess = HttpAccess.getInstance();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}
